<?php
/**
 * lbs计算距离
 */
class Distance {
	
	 /**
	 *计算某个经纬度的周围某段距离的正方形的四个点
	 *@param lng float 经度
	 *@param lat float 纬度
	 *@param distance float 该点所在圆的半径，该圆与此正方形内切，默认值为0.5千米
	 *@return array 正方形的四个点的经纬度坐标
	 */
	 function returnSquarePoint($lng, $lat,$distance = 0.5){
	 	define(EARTH_RADIUS, 6371);//地球半径，平均半径为6371km
		$dlng =  2 * asin(sin($distance / (2 * EARTH_RADIUS)) / cos(deg2rad($lat)));
		$dlng = rad2deg($dlng);
		
		$dlat = $distance/EARTH_RADIUS;
		$dlat = rad2deg($dlat);
		
		return array(
			'left-top'=>array('lat'=>$lat + $dlat,'lng'=>$lng-$dlng),
			'right-top'=>array('lat'=>$lat + $dlat, 'lng'=>$lng + $dlng),
			'left-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng - $dlng),
			'right-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng + $dlng)
		);
	 }
	 
	 function rad($d){
	 	return $d * 3.1415926535898 / 180.0;
	 }
	 
	 function GetDistance($lat1, $lng1, $lat2, $lng2){
	 	$EARTH_RADIUS = 6378.137;
	 	$radLat1 = $this->rad($lat1);
	 	//echo $radLat1;
	 	$radLat2 = $this->rad($lat2);
	 	$a = $radLat1 - $radLat2;
	 	$b = $this->rad($lng1) - $this->rad($lng2);
	 	$s = 2 * asin(sqrt(pow(sin($a/2),2) +
	 			cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)));
	 	$s = $s *$EARTH_RADIUS;
	 	$s = round($s * 10000) / 10000;
	 	return $s;
	 }
}
?>